var datafile_conf = require('../config').filedata;
var https = require('https');
var utils =  {
    remove_src_host : function (data) {
        return data.replaceAll(datafile_conf.host + datafile_conf.url, datafile_conf.url);
    },

    add_src_host : function (data) {
        return data.replaceAll(datafile_conf.url, datafile_conf.host + datafile_conf.url)
    },

    download: async function (url) {
        return new Promise((resolve, reject) => {
            https.get(url, (res) => {
                let image_data = "";
                res.setEncoding("binary");
                res.on('data', (chunk) => {
                    image_data += chunk;
                });
                res.on('end', () => {
                    return resolve(image_data);
                });
            }).on("error", function () {
                reject("error");
            });
        }).then(data => {
            return [null, data];
        }).catch(error => {
            return [error, null];
        })

    },
}
module.exports = utils;
